<?php
/*
	出库日志
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'手动出库'),
	array('op'=>'order','title'=>'销售出库'),
);
//声明列表页
if($_RQ['op']=='list'){
	//初始化返回前端的筛选条件数组
	$return['params']=array();
	//整理列表数据查询条件
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>0);
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
		$return['params']['goods_id']=$_RQ['goods_id'];
	}
	if($_RQ['store_id']){
		$params['store_id']=intval($_RQ['store_id']);
		$return['params']['store_id']=$_RQ['store_id'];
	}
	if($_RQ['outtype']){
		$params['outtype']=intval($_RQ['outtype']);
		$return['params']['outtype']=$_RQ['outtype'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('log',$parameter,$total);
	if($list){
		foreach($list as &$item){
			if($item['goodstype']==1){
				$item['goods']=$this->pdo_get('goods',array('id'=>$item['goods_id']),array('title','image'));
			}else{
				$item['goods']=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('title','image'));
			}
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	//将查询到的结果数组放入$return以备传回前端
	$return['list']=$list;
	//整理分页
	$return['totalpage'] =intval(ceil($total/$psize));
	$return['params']['page'] =$pindex;
	//声明列表页样式数组
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',//声明导出动作op，如无需导出功能，可不声明该字段
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			//列表展示列
			array('type'=>'member','name'=>'member','title'=>'操作员',),
			array('type'=>'select','name'=>'store_id','title'=>'门店','options'=>'stores'),
			array('type'=>'radio','name'=>'outtype','title'=>'出库类型','options'=>'outtype'),
			array('type'=>'string','name'=>'goods.title','title'=>'商品名称',),
			array('type'=>'image','name'=>'goods.image','title'=>'商品图片',),
			array('type'=>'string','name'=>'num','title'=>'出库数量',),
			array('type'=>'string','name'=>'remark','title'=>'备注',),
			array('type'=>'time','name'=>'createtime','title'=>'操作时间',),
		)),
		'search'=>array(
			//列表查询表单
			array('type'=>'select','width'=>15,'name'=>'store_id','title'=>'门店','options'=>'stores'),
			array('type'=>'radio','width'=>15,'name'=>'outtype','title'=>'出库类型','options'=>'outtype'),
			array('type'=>'choose','width'=>15,'name'=>'goods_id','title'=>'选择商品','action'=>'jy_weishop/goods.get_goods/'),
			array('name'=>'createtime','width'=>15,'title'=>'操作时间','type'=>'time_range'),
		),
	);
	$return['outtype']=$this->outtype;
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	//把数据返回前端
	exi($return);
}
if($_RQ['op']=='order'){
	//初始化返回前端的筛选条件数组
	$return['params']=array();
	//整理列表数据查询条件
	$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1);
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}else{
		$params['storeid >']=0;
	}
	if($_RQ['goodsid']){
		$params['goodsid']=intval($_RQ['goodsid']);
		$return['params']['goodsid']=$_RQ['goodsid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	//执行分页查询
	$list=pdo_getslice('jy_weishop_order_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['ordersn']=pdo_getcolumn('jy_weishop_order',array('id'=>$item['orderid']),'ordersn');
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['goods']=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']),array('title','image'));
		}
	}
	//将查询到的结果数组放入$return以备传回前端
	$return['list']=$list;
	//整理分页
	$return['totalpage'] =intval(ceil($total/$psize));
	$return['params']['page'] =$pindex;
	//声明列表页样式数组
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export2',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			//列表展示列
			array('type'=>'member','name'=>'member','title'=>'客户',),
			array('type'=>'select','name'=>'storeid','title'=>'门店','options'=>'stores'),
			array('type'=>'string','name'=>'goods.title','title'=>'商品名称',),
			array('type'=>'image','name'=>'goods.image','title'=>'商品图片',),
			array('type'=>'string','name'=>'total','title'=>'出库数量',),
			array('type'=>'time','name'=>'createtime','title'=>'操作时间',),
		)),
		'search'=>array(
			//列表查询表单
			array('type'=>'select','width'=>15,'name'=>'storeid','title'=>'门店','options'=>'stores'),
			array('type'=>'choose','width'=>15,'name'=>'goodsid','title'=>'选择商品','action'=>'jy_weishop/goods.get_goods/'),
			array('name'=>'createtime','width'=>15,'title'=>'操作时间','type'=>'time_range'),
		),
	);
	$return['outtype']=$this->outtype;
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	//把数据返回前端
	exi($return);
}
//导出动作
if($_RQ['op']=='export'){
	//整理筛选条件
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>0);
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	//声明需要查询哪些字段，为空表示获取所有
	$fields=array();
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('log',$parameter);
	if($list){
		foreach($list as &$item){
			//整理从数据库获取到的数据
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goods_id']),array('title','productsn','unit'));
			$item=array_merge($goods,$item);
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['user_name']=$item['member']['nickname'];
		}
	}
	//声明表头和对应字段
	$header=array(
		'title'=>'商品名称',
		'user_name'=>'客户',
		'num'=>'出库数量',
		'unit'=>'单位',
		'createtime'=>'出库时间',
		'remark'=>'备注',
	);
	//执行导出
	model('core/tools')->export2excel($header,$list,'出库日志');
}
if($_RQ['op']=='export2'){
	//整理筛选条件
	$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1);
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}else{
		$params['storeid >']=0;
	}
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	//声明需要查询哪些字段，为空表示获取所有
	$fields=array();
	//执行分页查询
	$list=pdo_getall('jy_weishop_order_goods',$params,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['ordersn']=pdo_getcolumn('jy_weishop_order',array('id'=>$item['orderid']),'ordersn');
			$item['member']=cfc('core')->member_show($item['mid']);
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']),array('title','unit','image'));
			$item=array_merge($goods,$item);
			$item['user_name']=$item['member']['nickname'];
		}
	}
	//声明表头和对应字段
	$header=array(
		'title'=>'商品名称',
		'user_name'=>'客户',
		'total'=>'销售数量',
		'unit'=>'单位',
		'createtime'=>'出库时间',
	);
	//执行导出
	model('core/tools')->export2excel($header,$list,'销售出库日志');
}